/**
 * 捕获 SIGCHLD 信号来会收子进程，避免僵尸进程产生
*/

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

#include <sys/wait.h>
#include <unistd.h>

void signal_handler(int signum)
{

    printf("%d receive signale %d\n", getpid(), signum);
    wait(0);
}

int main(void)
{

    signal(SIGCHLD, signal_handler);

    pid_t pid = fork();

    if (0 > pid)
    {
        perror("fork error: ");
        exit(EXIT_FAILURE);
    }
    else if (0 == pid)
    {
        printf("I am child progress %d\n", getpid());
        sleep(3);
        printf("child progress %d exit\n", getpid());
        exit(EXIT_SUCCESS);
    }

    int i = 0;
    while (i++ < 2000)
    {
        printf("I am parent progress %d\n", getpid());
        sleep(1);
    }
    return 0;
}